class ByteListToCode:
    def __init__(self, file_path):
        self.file_path = file_path

    def load_lzw_compressed_bytelist(self):
        with open(self.file_path, 'rb') as f:
            self.lzw_compressed_bytelist = f.read()
        return self
    
    def bytelist_to_codelist(self):
        total_binary_string = ''
        for b in reversed(self.lzw_compressed_bytelist):
            binary_string = ''.join(format(b, '08b'))
            total_binary_string = total_binary_string + binary_string
        print(total_binary_string)

        MiniCodeSize = 2
        size = MiniCodeSize + 1

        index = len(total_binary_string)


        color_size = 4
        # 创建字典
        dictionary = {f"{i}":i for i in range(0,color_size)}
        dictionary["clear"] = len(dictionary)
        dictionary["end"] = len(dictionary)

        print(dictionary)
        result = []

        whileCount = 0
        while True:           
            bit3_str = total_binary_string[index-size:index]
            index -= size   
            num = int(bit3_str, 2)
            print(bit3_str, num)   
            whileCount += 1                                 
            if whileCount >=2:
                break
        print("whileCount", whileCount, "index", index, "num", num)

        
        return self     
    
    def print(self):
        pass


ByteListToCode('gif_text_data_lzw_compressed.bin').load_lzw_compressed_bytelist().bytelist_to_codelist().print()